<?php

namespace app\common\lib\rtc;

use app\aliyun\controller\AliyunRtc;
use app\chat\LiveMessage;
use app\common\lib\Util;
use app\common\model\User;
use app\zyhsapi\model\ZyhsConsultChatRecordDetails;
use app\zyhsapi\model\ZyhsConsultChatRtcRecord;
use app\zyhsapi\model\ZyhsConsultRtcUserBenefits;
use app\zyhsapi\model\ZyhsUser;

class ConsultSubscribe extends RtcCommon
{
    /**
     * 入会
     * @param array $userData
     * @param string $channelId 渠道ID
     * @return void
     * @throws \Exception
     */
    public function Join(array $userData, string $channelId)
    {
        $consultChatRtcRecordModel = new ZyhsConsultChatRtcRecord();
        $rtcInfo = $consultChatRtcRecordModel->getInfoById($channelId);

        if ($userData['type'] === 1 && $userData['uid'] != $rtcInfo['uid'])
        {   // 非法连线用户
            $this->removeTerminals('consult_' . $channelId, 'user_' . $userData['uid']);
            return;
        } elseif ($userData['type'] === 2 && $userData['uid'] != $rtcInfo['astr_id'])
        {   // 非法连线用户
            $this->removeTerminals('consult_' . $channelId, 'astr_' . $userData['uid']);
            return;
        }
        $appType = $rtcInfo['app_type'] ?? 1;
        $astrNowConsultRtcInfo = $consultChatRtcRecordModel->getNowRtcDataByCache(2, $appType, $rtcInfo['astr_id']);

        if (!$astrNowConsultRtcInfo || $astrNowConsultRtcInfo['status'] < 2)
        {
            $this->removeTerminals('consult_' . $channelId, 'user_' . $userData['uid']);
            return;
        }
        if ($userData['type'] === 1)     // -1 未知用户  1=>准了APP用户  2=>占星师
        {
            if (isset($astrNowConsultRtcInfo['user_rtc_online']) && $astrNowConsultRtcInfo['user_rtc_online'])
            {   // 已经在线了 不处理
                return;
            }
            $updata = ['user_rtc_online' => 1];
            $astrNowConsultRtcInfo['user_rtc_online'] = 1;
        } else
        {
            if (isset($astrNowConsultRtcInfo['astr_rtc_online']) && $astrNowConsultRtcInfo['astr_rtc_online'])
            {   // 已经在线了 不处理
                return;
            }
            $updata = ['astr_rtc_online' => 1];
            $astrNowConsultRtcInfo['astr_rtc_online'] = 1;
        }

        if (isset($astrNowConsultRtcInfo['user_rtc_online'], $astrNowConsultRtcInfo['astr_rtc_online']) && $astrNowConsultRtcInfo['user_rtc_online'] && $astrNowConsultRtcInfo['astr_rtc_online'])
        {   // 用户 和 咨询师 都在线 连接成功
            $updata['status'] = 3;  // 连线中

            if ($rtcInfo && !in_array($rtcInfo['status'], [2, 3], true))
            {
                $this->removeTerminals('consult_' . $channelId, 'user_' . $userData['uid']);
                return;
            }
            $nowTime = time();
            $rtcUpdate = [
                'status' => 3,  // 2=>连接中 3=>连麦中 4=>连麦结束 5=>停止连麦（未接听） 6=>故障挂断
                'start_time' => $nowTime,
            ];
            if (isset($rtcInfo['extend']['free_rtc_duration']) && $rtcInfo['extend']['free_rtc_duration'])
            {   // 本次连线 准备使用新人福利
                // 获取新人福利 免充值 发起连线
                $zyhsConsultRtcUserBenefitsModel = new ZyhsConsultRtcUserBenefits();
                $newUserWelfare = $zyhsConsultRtcUserBenefitsModel->getConsultRtcUserBenefits($appType, $rtcInfo['uid']);
                if ($newUserWelfare)
                {   // 使用新人福利
                    $res = $zyhsConsultRtcUserBenefitsModel->updateData($newUserWelfare['id'], $appType, $rtcInfo['uid'], [
                        'rtc_id' => $rtcInfo['id'],
                        'use_time' => $nowTime,
                    ]);

                    if (!$res)
                    {   // 使用新人福利失败
                        // 免费连线时长   清零
                        $rtcInfo['extend']['free_rtc_duration'] = 0;
                        // 福利ID     不使用福利
                        $rtcInfo['extend']['bid'] = 0;
                        $rtcUpdate['extend'] = $rtcInfo['extend'];
                    }
                }
            }
            $consultChatRtcRecordModel->updateData((int)$channelId, $appType, $astrNowConsultRtcInfo['uid'], $rtcUpdate, 1);
            //  更新 用户信息
            $consultChatRtcRecordModel->saveNowRtcDataToCache(1, $appType, $rtcInfo['uid'], $updata, true, $rtcInfo['chat_detail_id']);
            //  更新 咨询师信息
            $consultChatRtcRecordModel->saveNowRtcDataToCache(2, $appType, $rtcInfo['astr_id'], $updata, true, $rtcInfo['chat_detail_id']);
            $websocketWho = $appType === 1 ? LiveMessage::WHO_TYPE_ZYHS_UID : LiveMessage::WHO_TYPE_UID;
            // 向用户发送 发送消息 ----------------------------------------------------------------
            // 1 连麦消息
            $popConsultMsg = [
                'msg_type' => 1,
                'astr_id' => $rtcInfo['astr_id'],
                'msg_data' => [
                    'id' => $rtcInfo['chat_detail_id'],
                    'type' => 1,
                    'time_des' => '',
                    'time' => '',
                    'avatar' => $astrNowConsultRtcInfo['avatar'],
                    'msg_des' => '连线咨询',
                    'tags' => [],
                    'consult_info' => [
                        'status' => 3,
                        'tip' => '语音咨询中',
                        'target' => 'local',
                        'link' => "alert_consult_question?id={$rtcInfo['chat_detail_id']}&need_auth=2",
                    ],
                    'btn_info' => null,
                    'content' => '',
                    'content_type' => 1,
                ],
            ];
            LiveMessage::sendMsg(LiveMessage::who($websocketWho, 0, $astrNowConsultRtcInfo['uid']), LiveMessage::MSG_TYPE_POP_CONSULT_MSG, $popConsultMsg);
            // 2 连麦状态刷新消息
            $consultDetailRefresh = [
                'target' => 'local',
                'link' => "alert_consult_question?id={$rtcInfo['chat_detail_id']}&need_auth=2",
                'force_open' => 1,
            ];
            LiveMessage::sendMsg(LiveMessage::who($websocketWho, 0, $astrNowConsultRtcInfo['uid']), LiveMessage::MSG_TYPE_POP_CONSULT_DETAIL_REFRESH, $consultDetailRefresh);
            // 向咨询师 发送消息 --------------------------------
            // 1 连麦消息
            $popConsultMsg = [
                'msg_type' => 1,
                'app_type' => $appType,
                'uid' => $astrNowConsultRtcInfo['uid'],
                'msg_data' => [
                    'id' => $rtcInfo['chat_detail_id'],
                    'type' => 2,
                    'time_des' => '',
                    'time' => '',
                    'avatar' => $astrNowConsultRtcInfo['avatar'],
                    'msg_des' => '连线咨询',
                    'tags' => [],
                    'consult_info' => [
                        'status' => 3,
                        'tip' => '语音咨询中',
                        'target' => 'local',
                        'link' => "alert_consult_question?id={$rtcInfo['chat_detail_id']}&need_auth=2",
                    ],
                    'btn_info' => null,
                    'content' => '',
                    'content_type' => 1,
                ],
            ];
            LiveMessage::sendMsg(LiveMessage::who(LiveMessage::WHO_TYPE_ASTRO, 0, $rtcInfo['astr_id']), LiveMessage::MSG_TYPE_POP_CONSULT_MSG, $popConsultMsg);
            // 2 连麦状态刷新消息
            LiveMessage::sendMsg(LiveMessage::who(LiveMessage::WHO_TYPE_ASTRO, 0, $rtcInfo['astr_id']), LiveMessage::MSG_TYPE_POP_CONSULT_DETAIL_REFRESH, $consultDetailRefresh);
            // 3 隐藏电话图标
            $voiceCallData = [
                'chat_id' => $rtcInfo['chat_detail_id'],
                'app_type' => $appType,
                'uid' => $rtcInfo['uid'],
                'avatar' => $astrNowConsultRtcInfo['avatar'],
                'nickname' => $astrNowConsultRtcInfo['nickname'],
                'status' => 0,
                'desc' => '',
            ];
            LiveMessage::sendMsg(LiveMessage::who(LiveMessage::WHO_TYPE_ASTRO, 0, $rtcInfo['astr_id']), LiveMessage::MSG_TYPE_POP_VOICE_CONSULT_CALL, $voiceCallData);
            // 新增 准币扣费队列
            Util::queuePush('rtc_consult_deduct_coin', ['id' => $rtcInfo['chat_detail_id']], 60);  // 60秒后 扣除准币
            return;
        }
        $consultChatRtcRecordModel->saveNowRtcDataToCache(2, $appType, $rtcInfo['astr_id'], $updata, true, $rtcInfo['chat_detail_id']);
    }

    /**
     * 入会
     * @param array $userData
     * @param string $channelId 渠道ID
     * @return void
     * @throws \Exception
     */
    public function Join2(array $userData, string $channelId)
    {
        $consultChatRtcRecordModel = new ZyhsConsultChatRtcRecord();
        $consultChatRecordDetailsModel = new ZyhsConsultChatRecordDetails();
        $chatDetail = $consultChatRecordDetailsModel->getInfoById($channelId);
        if ($userData['type'] === 1 && $userData['uid'] != $chatDetail['uid'])
        {   // 非法连线用户
            $this->removeTerminals('consult2_' . $channelId, 'user_' . $userData['uid']);
            return;
        } elseif ($userData['type'] === 2 && $userData['uid'] != $chatDetail['astr_id'])
        {   // 非法连线用户
            $this->removeTerminals('consult2_' . $channelId, 'astr_' . $userData['uid']);
            return;
        }
        $appType = $chatDetail['app_type'] ?? 1;
        $astrNowConsultRtcInfo = $consultChatRtcRecordModel->getNowRtcDataByCache(2, $appType, $chatDetail['astr_id']);
        if (!$astrNowConsultRtcInfo)
        {
            if ($userData['type'] === 1)
            {   // 非法连线用户
                $this->removeTerminals('consult2_' . $channelId, 'user_' . $userData['uid']);
            } elseif ($userData['type'] === 2)
            {   // 非法连线用户
                $this->removeTerminals('consult2_' . $channelId, 'astr_' . $userData['uid']);
            }
            return;
        }
        if ($userData['type'] === 1)     // -1 未知用户  1=>准了APP用户  2=>占星师
        {
            if (isset($astrNowConsultRtcInfo['user_rtc_online']) && $astrNowConsultRtcInfo['user_rtc_online'])
            {   // 已经在线了 不处理
                return;
            }
            $updata = ['user_rtc_online' => 1];
            $astrNowConsultRtcInfo['user_rtc_online'] = 1;
        } else
        {
            if (isset($astrNowConsultRtcInfo['astr_rtc_online']) && $astrNowConsultRtcInfo['astr_rtc_online'])
            {   // 已经在线了 不处理
                return;
            }
            $updata = ['astr_rtc_online' => 1];
            $astrNowConsultRtcInfo['astr_rtc_online'] = 1;
        }

        if (isset($astrNowConsultRtcInfo['user_rtc_online'], $astrNowConsultRtcInfo['astr_rtc_online']) && $astrNowConsultRtcInfo['user_rtc_online'] && $astrNowConsultRtcInfo['astr_rtc_online'])
        {   // 用户 和 咨询师 都在线 连接成功
            $rtcInfo = $consultChatRtcRecordModel->getInfoById($chatDetail['extend']['rtc_data']['rtc_id']);
            $updata['status'] = 3;  // 连线中
            if ($rtcInfo && !in_array($rtcInfo['status'], [2, 3], true))
            {
                if ($userData['type'] === 1)
                {   // 非法连线用户
                    $this->removeTerminals('consult2_' . $channelId, 'user_' . $userData['uid']);
                } elseif ($userData['type'] === 2)
                {   // 非法连线用户
                    $this->removeTerminals('consult2_' . $channelId, 'astr_' . $userData['uid']);
                }
                return;
            }
            $nowTime = time();
            $rtcUpdate = [
                'status' => 3,  // 2=>连接中 3=>连麦中 4=>连麦结束 5=>停止连麦（未接听） 6=>故障挂断
                'start_time' => $nowTime,
            ];
            if (isset($rtcInfo['extend']['free_rtc_duration']) && $rtcInfo['extend']['free_rtc_duration'])
            {   // 本次连线 准备使用新人福利
                // 获取新人福利 免充值 发起连线
                $zyhsConsultRtcUserBenefitsModel = new ZyhsConsultRtcUserBenefits();
                $newUserWelfare = $zyhsConsultRtcUserBenefitsModel->getConsultRtcUserBenefits($appType, $rtcInfo['uid']);
                if ($newUserWelfare)
                {   // 使用新人福利
                    $res = $zyhsConsultRtcUserBenefitsModel->updateData($newUserWelfare['id'], $appType, $rtcInfo['uid'], [
                        'rtc_id' => $rtcInfo['id'],
                        'use_time' => $nowTime,
                    ]);

                    if (!$res)
                    {   // 使用新人福利失败
                        // 免费连线时长   清零
                        $rtcInfo['extend']['free_rtc_duration'] = 0;
                        // 福利ID     不使用福利
                        $rtcInfo['extend']['bid'] = 0;
                        $rtcUpdate['extend'] = $rtcInfo['extend'];
                    }
                }
            }
            $consultChatRtcRecordModel->updateData($rtcInfo['id'], $appType, $astrNowConsultRtcInfo['uid'], $rtcUpdate, 1);
            //  更新 用户信息
            $consultChatRtcRecordModel->saveNowRtcDataToCache(1, $appType, $rtcInfo['uid'], $updata, true, $channelId);
            //  更新 咨询师信息
            $consultChatRtcRecordModel->saveNowRtcDataToCache(2, $appType, $rtcInfo['astr_id'], $updata, true, $channelId);
            $websocketWho = $appType === 1 ? LiveMessage::WHO_TYPE_ZYHS_UID : LiveMessage::WHO_TYPE_UID;
            // 向用户发送 发送消息 ----------------------------------------------------------------
            // 1 连麦消息
            $popConsultMsg = [
                'msg_type' => 1,
                'astr_id' => $rtcInfo['astr_id'],
                'msg_data' => [
                    'id' => $rtcInfo['chat_detail_id'],
                    'type' => 1,
                    'time_des' => '',
                    'time' => '',
                    'avatar' => $astrNowConsultRtcInfo['avatar'],
                    'msg_des' => '连线咨询',
                    'tags' => [],
                    'consult_info' => [
                        'status' => 3,
                        'tip' => '语音咨询中',
                        'target' => 'local',
                        'link' => "alert_consult_question?id={$rtcInfo['chat_detail_id']}&need_auth=2",
                    ],
                    'btn_info' => null,
                    'content' => '',
                    'content_type' => 1,
                ],
            ];
            LiveMessage::sendMsg(LiveMessage::who($websocketWho, 0, $astrNowConsultRtcInfo['uid']), LiveMessage::MSG_TYPE_POP_CONSULT_MSG, $popConsultMsg);
            // 2 连麦状态刷新消息
            $consultDetailRefresh = [
                'target' => 'local',
                'link' => "alert_consult_question?id={$rtcInfo['chat_detail_id']}&need_auth=2",
                'force_open' => 1,
            ];
            LiveMessage::sendMsg(LiveMessage::who($websocketWho, 0, $astrNowConsultRtcInfo['uid']), LiveMessage::MSG_TYPE_POP_CONSULT_DETAIL_REFRESH, $consultDetailRefresh);
            // 向咨询师 发送消息 --------------------------------
            // 1 连麦消息
            $popConsultMsg = [
                'msg_type' => 1,
                'app_type' => $appType,
                'uid' => $astrNowConsultRtcInfo['uid'],
                'msg_data' => [
                    'id' => $rtcInfo['chat_detail_id'],
                    'type' => 2,
                    'time_des' => '',
                    'time' => '',
                    'avatar' => $astrNowConsultRtcInfo['avatar'],
                    'msg_des' => '连线咨询',
                    'tags' => [],
                    'consult_info' => [
                        'status' => 3,
                        'tip' => '语音咨询中',
                        'target' => 'local',
                        'link' => "alert_consult_question?id={$rtcInfo['chat_detail_id']}&need_auth=2",
                    ],
                    'btn_info' => null,
                    'content' => '',
                    'content_type' => 1,
                ],
            ];
            LiveMessage::sendMsg(LiveMessage::who(LiveMessage::WHO_TYPE_ASTRO, 0, $rtcInfo['astr_id']), LiveMessage::MSG_TYPE_POP_CONSULT_MSG, $popConsultMsg);
            // 2 连麦状态刷新消息
            LiveMessage::sendMsg(LiveMessage::who(LiveMessage::WHO_TYPE_ASTRO, 0, $rtcInfo['astr_id']), LiveMessage::MSG_TYPE_POP_CONSULT_DETAIL_REFRESH, $consultDetailRefresh);
            // 3 隐藏电话图标
            $voiceCallData = [
                'chat_id' => $rtcInfo['chat_detail_id'],
                'app_type' => $appType,
                'uid' => $rtcInfo['uid'],
                'avatar' => $astrNowConsultRtcInfo['avatar'],
                'nickname' => $astrNowConsultRtcInfo['nickname'],
                'status' => 0,
                'desc' => '',
            ];
            LiveMessage::sendMsg(LiveMessage::who(LiveMessage::WHO_TYPE_ASTRO, 0, $rtcInfo['astr_id']), LiveMessage::MSG_TYPE_POP_VOICE_CONSULT_CALL, $voiceCallData);
            // 新增 准币扣费队列
            Util::queuePush('rtc_consult_deduct_coin', ['id' => $rtcInfo['chat_detail_id']], 60);  // 60秒后 扣除准币
            return;
        }
        $consultChatRtcRecordModel->saveNowRtcDataToCache(2, $appType, $chatDetail['astr_id'], $updata, true, $channelId);
    }

    /**
     * 离会
     * @param array $userData
     * @param string $channelId
     * @return void
     * @throws \Exception
     */
    public function Leave(array $userData, string $channelId)
    {
        $nowTime = time();
        $consultChatRtcRecordModel = new ZyhsConsultChatRtcRecord();
        $consultChatRecordDetailsModel = new ZyhsConsultChatRecordDetails();
        $rtcInfo = $consultChatRtcRecordModel->getInfoById($channelId);
        $appType = $rtcInfo['app_type'] ?? 1;
        $astrNowConsultRtcInfo = $consultChatRtcRecordModel->getNowRtcDataByCache(2, $appType, $rtcInfo['astr_id']);
        if (!$astrNowConsultRtcInfo || $astrNowConsultRtcInfo['chat_id'] !== $rtcInfo['chat_detail_id'])
        {
            return;
        }
        if (!in_array($rtcInfo['status'], [2, 3], true))
        {
            return;
        }
        $chatDetail = $consultChatRecordDetailsModel->getInfoById($rtcInfo['chat_detail_id']);

        if ($appType === 1)
        {   // 准有好事
            $websocketWho = LiveMessage::WHO_TYPE_ZYHS_UID;
            $userInfo = (new ZyhsUser())->getInfoById($chatDetail['uid']);
        } else
        {   // 准了APP
            $websocketWho = LiveMessage::WHO_TYPE_UID;
            $userInfo = (new User())->getUserinfo(['id' => $chatDetail['uid']]);
        }

        if ($rtcInfo['status'] === 2)
        {   // 连接中  1=>准了APP用户  2=>占星师
            $rtcInfoUpdate = [
                'type' => $userData['type'],
                'end_time' => $nowTime,
                'status' => 6,
            ];
            // 更新了连麦状态
            $consultChatRtcRecordModel->updateData($rtcInfo['id'], $appType, $rtcInfo['uid'], $rtcInfoUpdate, 2);

            $consultChatRtcRecordModel->delNowRtcDataByCache(2, $appType, $rtcInfo['astr_id'], $rtcInfo['chat_detail_id']);
            // 删除用户自己的 连线信息
            $userNowConsultRtcInfo = $consultChatRtcRecordModel->getNowRtcDataByCache(1, $appType, $rtcInfo['uid']);
            if ($userNowConsultRtcInfo && $userNowConsultRtcInfo['chat_id'] != $rtcInfo['chat_detail_id'])
            {
                $consultChatRtcRecordModel->delNowRtcDataByCache(1, $appType, $rtcInfo['uid'], $rtcInfo['chat_detail_id']);
            }
            $msg = $userData['type'] === 1 ? '连线已取消' : '咨询师不便接听，稍后重试';
            // 1、 向 用户 发送消息
            $popUserConsultMsg = [
                'msg_type' => 1,
                'astr_id' => $rtcInfo['astr_id'],
                'msg_data' => [
                    'id' => $rtcInfo['chat_detail_id'],
                    'type' => 1,
                    'time_des' => '',
                    'time' => '',
                    'avatar' => $userInfo['avatar'],
                    'msg_des' => '连线咨询',
                    'tags' => [],
                    'consult_info' => [
                        'status' => 4,
                        'tip' => $msg,
                        'target' => 'local',
                        'link' => 'empty',
                    ],
                    'btn_info' => null,
                    'content' => '',
                    'content_type' => 1,
                ],
            ];
            // 向 咨询师 发送消息
            $popAstrConsultMsg = [
                'msg_type' => 1,
                'app_type' => $appType,
                'uid' => $rtcInfo['uid'],
                'msg_data' => [
                    'id' => $rtcInfo['chat_detail_id'],
                    'type' => 1,
                    'time_des' => '',
                    'time' => '',
                    'avatar' => $userInfo['avatar'],
                    'msg_des' => '连线咨询',
                    'tags' => [],
                    'consult_info' => [
                        'status' => 4,
                        'tip' => '连线已取消',
                        'target' => 'local',
                        'link' => 'empty',
                    ],
                    'btn_info' => null,
                    'content' => '',
                    'content_type' => 1,
                ],
            ];
            // 2、 发通知
            $consultMsg = [
                'content' => $msg,
            ];
            LiveMessage::sendMsg(LiveMessage::who($websocketWho, 0, $astrNowConsultRtcInfo['uid']), LiveMessage::MSG_TYPE_POP_CONSULT_MSG, $popUserConsultMsg);
            LiveMessage::sendMsg(LiveMessage::who(LiveMessage::WHO_TYPE_ASTRO, 0, $chatDetail['astr_id']), LiveMessage::MSG_TYPE_POP_CONSULT_MSG, $popAstrConsultMsg);
            LiveMessage::sendMsg(LiveMessage::who($websocketWho, 0, $astrNowConsultRtcInfo['uid']), LiveMessage::MSG_TYPE_POP_CONSULT_TOAST, $consultMsg);
            LiveMessage::sendMsg(LiveMessage::who(LiveMessage::WHO_TYPE_ASTRO, 0, $chatDetail['astr_id']), LiveMessage::MSG_TYPE_POP_CONSULT_TOAST, $consultMsg);
        } else
        {   // 连线中
            // 咨询师 收益结算
            $consultChatRtcRecordModel->rtcConsultHangUp($chatDetail, '', $userData['type'], 2);
//            $msg = '连线已挂断';
//            // 1、 向 用户 发送消息
//            $popUserConsultMsg = [
//                'msg_type' => 1,
//                'astr_id' => $rtcInfo['astr_id'],
//                'msg_data' => [
//                    'id' => $rtcInfo['chat_detail_id'],
//                    'type' => 1,
//                    'time_des' => '',
//                    'time' => '',
//                    'avatar' => $userInfo['avatar'],
//                    'msg_des' => '连线咨询',
//                    'tags' => [],
//                    'consult_info' => [
//                        'status' => 4,
//                        'tip' => "连线时长 02:00，消耗100准币",
//                        'target' => 'local',
//                        'link' => 'empty',
//                    ],
//                    'btn_info' => null,
//                    'content' => '',
//                    'content_type' => 1,
//                ]
//            ];
//            // 向 咨询师 发送消息
//            $popAstrConsultMsg = [
//                'msg_type' => 1,
//                'uid' => $rtcInfo['uid'],
//                'msg_data' => [
//                    'id' => $rtcInfo['chat_detail_id'],
//                    'type' => 1,
//                    'time_des' => '',
//                    'time' => '',
//                    'avatar' => $userInfo['avatar'],
//                    'msg_des' => '连线咨询',
//                    'tags' => [],
//                    'consult_info' => [
//                        'status' => 4,
//                        'tip' => "连线时长 02:00，消耗100准币",
//                        'target' => 'local',
//                        'link' => 'empty',
//                    ],
//                    'btn_info' => [
//                        [
//                            'name' => '查看记录',
//                            'type' => 4,
//                            'id' => $rtcInfo['chat_detail_id'],
//                        ],
//                    ],
//                    'content' => null,
//                    'content_type' => 1,
//                ]
//            ];
        }

        (new AliyunRtc())->rmEventSubAndDelRtcChannel('consult_' . $channelId);
    }

    /**
     * 离会
     * @param array $userData
     * @param string $channelId
     * @return void
     * @throws \Exception
     */
    public function Leave2(array $userData, string $channelId)
    {
        $nowTime = time();
        $consultChatRtcRecordModel = new ZyhsConsultChatRtcRecord();
        $consultChatRecordDetailsModel = new ZyhsConsultChatRecordDetails();
        $chatDetail = $consultChatRecordDetailsModel->getInfoById($channelId);
        $appType = $chatDetail['app_type'] ?? 1;
        if ($appType === 1)
        {   // 准有好事
            $websocketWho = LiveMessage::WHO_TYPE_ZYHS_UID;
            $userInfo = (new ZyhsUser())->getInfoById($chatDetail['uid']);
        } else
        {   // 准了APP
            $websocketWho = LiveMessage::WHO_TYPE_UID;
            $userInfo = (new User())->getUserinfo(['id' => $chatDetail['uid']]);
        }
        $rtcInfo = $chatDetail['extend']['rtc_data']['rtc_id'] ? $consultChatRtcRecordModel->getInfoById($chatDetail['extend']['rtc_data']['rtc_id']) : [];
        if ($rtcInfo)
        {
            if (!in_array($rtcInfo['status'], [2, 3], true))
            {
                return;
            }
            if ($rtcInfo['status'] === 2)
            {   // 连接中  1=>准了APP用户  2=>占星师
                $rtcInfoUpdate = [
                    'type' => $userData['type'],
                    'end_time' => $nowTime,
                    'status' => 6,
                ];
                // 更新了连麦状态
                $consultChatRtcRecordModel->updateData($rtcInfo['id'], $appType, $rtcInfo['uid'], $rtcInfoUpdate, 2);

                $consultChatRtcRecordModel->delNowRtcDataByCache(2, $appType, $rtcInfo['astr_id'], $rtcInfo['chat_detail_id']);
                // 删除用户自己的 连线信息
                $userNowConsultRtcInfo = $consultChatRtcRecordModel->getNowRtcDataByCache(1, $appType, $rtcInfo['uid']);
                if ($userNowConsultRtcInfo && $userNowConsultRtcInfo['chat_id'] == $rtcInfo['chat_detail_id'])
                {
                    $consultChatRtcRecordModel->delNowRtcDataByCache(1, $appType, $rtcInfo['uid'], $rtcInfo['chat_detail_id']);
                }
                $msg = $userData['type'] === 1 ? '连线已取消' : '咨询师不便接听，稍后重试';
                // 1、 向 用户 发送消息
                $popUserConsultMsg = [
                    'msg_type' => 1,
                    'astr_id' => $rtcInfo['astr_id'],
                    'msg_data' => [
                        'id' => $rtcInfo['chat_detail_id'],
                        'type' => 1,
                        'time_des' => '',
                        'time' => '',
                        'avatar' => $userInfo['avatar'],
                        'msg_des' => '连线咨询',
                        'tags' => [],
                        'consult_info' => [
                            'status' => 4,
                            'tip' => $msg,
                            'target' => 'local',
                            'link' => 'empty',
                        ],
                        'btn_info' => null,
                        'content' => '',
                        'content_type' => 1,
                    ],
                ];
                // 向 咨询师 发送消息
                $popAstrConsultMsg = [
                    'msg_type' => 1,
                    'app_type' => $appType,
                    'uid' => $rtcInfo['uid'],
                    'msg_data' => [
                        'id' => $rtcInfo['chat_detail_id'],
                        'type' => 1,
                        'time_des' => '',
                        'time' => '',
                        'avatar' => $userInfo['avatar'],
                        'msg_des' => '连线咨询',
                        'tags' => [],
                        'consult_info' => [
                            'status' => 4,
                            'tip' => '连线已取消',
                            'target' => 'local',
                            'link' => 'empty',
                        ],
                        'btn_info' => null,
                        'content' => '',
                        'content_type' => 1,
                    ],
                ];
                // 2、 发通知
                $consultMsg = [
                    'content' => $msg,
                ];
                LiveMessage::sendMsg(LiveMessage::who($websocketWho, 0, $chatDetail['uid']), LiveMessage::MSG_TYPE_POP_CONSULT_MSG, $popUserConsultMsg);
                LiveMessage::sendMsg(LiveMessage::who(LiveMessage::WHO_TYPE_ASTRO, 0, $chatDetail['astr_id']), LiveMessage::MSG_TYPE_POP_CONSULT_MSG, $popAstrConsultMsg);
                LiveMessage::sendMsg(LiveMessage::who($websocketWho, 0, $chatDetail['uid']), LiveMessage::MSG_TYPE_POP_CONSULT_TOAST, $consultMsg);
                LiveMessage::sendMsg(LiveMessage::who(LiveMessage::WHO_TYPE_ASTRO, 0, $chatDetail['astr_id']), LiveMessage::MSG_TYPE_POP_CONSULT_TOAST, $consultMsg);
            } else
            {   // 连线中
                // 咨询师 收益结算
                $consultChatRtcRecordModel->rtcConsultHangUp($chatDetail, '', $userData['type'], 2);
//            $msg = '连线已挂断';
//            // 1、 向 用户 发送消息
//            $popUserConsultMsg = [
//                'msg_type' => 1,
//                'astr_id' => $rtcInfo['astr_id'],
//                'msg_data' => [
//                    'id' => $rtcInfo['chat_detail_id'],
//                    'type' => 1,
//                    'time_des' => '',
//                    'time' => '',
//                    'avatar' => $userInfo['avatar'],
//                    'msg_des' => '连线咨询',
//                    'tags' => [],
//                    'consult_info' => [
//                        'status' => 4,
//                        'tip' => "连线时长 02:00，消耗100准币",
//                        'target' => 'local',
//                        'link' => 'empty',
//                    ],
//                    'btn_info' => null,
//                    'content' => '',
//                    'content_type' => 1,
//                ]
//            ];
//            // 向 咨询师 发送消息
//            $popAstrConsultMsg = [
//                'msg_type' => 1,
//                'uid' => $rtcInfo['uid'],
//                'msg_data' => [
//                    'id' => $rtcInfo['chat_detail_id'],
//                    'type' => 1,
//                    'time_des' => '',
//                    'time' => '',
//                    'avatar' => $userInfo['avatar'],
//                    'msg_des' => '连线咨询',
//                    'tags' => [],
//                    'consult_info' => [
//                        'status' => 4,
//                        'tip' => "连线时长 02:00，消耗100准币",
//                        'target' => 'local',
//                        'link' => 'empty',
//                    ],
//                    'btn_info' => [
//                        [
//                            'name' => '查看记录',
//                            'type' => 4,
//                            'id' => $rtcInfo['chat_detail_id'],
//                        ],
//                    ],
//                    'content' => null,
//                    'content_type' => 1,
//                ]
//            ];
            }
            // (new AliyunRtc())->rmEventSubAndDelRtcChannel('consult_' . $channelId);
        } else
        {   // 还未接听
            // 用户离线
            $chatDetail['extend']['rtc_data']['status'] = 4;  //【rtc_id=0该字段数据生效】 1=>申请中待接听  2=>咨询师未响应  3=>咨询师拒绝  4=>连线已取消 5=>咨询师忙线中
            // 更新数据
            $consultChatRecordDetailsModel->UpdateData($channelId, ['extend' => $chatDetail['extend']]);
            // 删除占星师的 连线信息
            $astrNowConsultRtcInfo = $consultChatRtcRecordModel->getNowRtcDataByCache(2, $appType, $chatDetail['astr_id']);
            if ($astrNowConsultRtcInfo && $astrNowConsultRtcInfo['chat_id'] == $channelId)
            {
                $consultChatRtcRecordModel->delNowRtcDataByCache(2, $appType, $chatDetail['astr_id'], $channelId);
                if (LiveMessage::getUserIsOnline(2, $chatDetail['astr_id']))
                {   //  隐藏 电话提醒
                    $voiceCallData = [
                        'chat_id' => $channelId,
                        'app_type' => $appType,
                        'uid' => $chatDetail['uid'],
                        'avatar' => $userInfo['avatar'],
                        'nickname' => $userInfo['nickname'],
                        'status' => 0,
                        'desc' => '',
                    ];
                    LiveMessage::sendMsg(LiveMessage::who(LiveMessage::WHO_TYPE_ASTRO, 0, $chatDetail['astr_id']), LiveMessage::MSG_TYPE_POP_VOICE_CONSULT_CALL, $voiceCallData);
                }
            }
            $consultChatRtcRecordModel->delNowRtcDataByCache(1, $appType, $chatDetail['uid'], $channelId);
            $msg = $userData['type'] === 1 ? '连线已取消' : '咨询师不便接听，稍后重试';
            // 1、 向 用户 发送消息
            $popUserConsultMsg = [
                'msg_type' => 1,
                'astr_id' => $chatDetail['astr_id'],
                'msg_data' => [
                    'id' => $channelId,
                    'type' => 1,
                    'time_des' => '',
                    'time' => '',
                    'avatar' => $userInfo['avatar'],
                    'msg_des' => '连线咨询',
                    'tags' => [],
                    'consult_info' => [
                        'status' => 4,
                        'tip' => $msg,
                        'target' => 'local',
                        'link' => 'empty',
                    ],
                    'btn_info' => null,
                    'content' => '',
                    'content_type' => 1,
                ],
            ];
            // 向 咨询师 发送消息
            $popAstrConsultMsg = [
                'msg_type' => 1,
                'app_type' => $appType,
                'uid' => $chatDetail['uid'],
                'msg_data' => [
                    'id' => $channelId,
                    'type' => 1,
                    'time_des' => '',
                    'time' => '',
                    'avatar' => $userInfo['avatar'],
                    'msg_des' => '连线咨询',
                    'tags' => [],
                    'consult_info' => [
                        'status' => 4,
                        'tip' => '连线已取消',
                        'target' => 'local',
                        'link' => 'empty',
                    ],
                    'btn_info' => null,
                    'content' => '',
                    'content_type' => 1,
                ],
            ];
            // 2、 发通知
            $consultMsg = [
                'content' => $msg,
            ];
            LiveMessage::sendMsg(LiveMessage::who($websocketWho, 0, $chatDetail['uid']), LiveMessage::MSG_TYPE_POP_CONSULT_MSG, $popUserConsultMsg);
            LiveMessage::sendMsg(LiveMessage::who(LiveMessage::WHO_TYPE_ASTRO, 0, $chatDetail['astr_id']), LiveMessage::MSG_TYPE_POP_CONSULT_MSG, $popAstrConsultMsg);
            LiveMessage::sendMsg(LiveMessage::who($websocketWho, 0, $chatDetail['uid']), LiveMessage::MSG_TYPE_POP_CONSULT_TOAST, $consultMsg);
            LiveMessage::sendMsg(LiveMessage::who(LiveMessage::WHO_TYPE_ASTRO, 0, $chatDetail['astr_id']), LiveMessage::MSG_TYPE_POP_CONSULT_TOAST, $consultMsg);
        }
    }
}